package cn.stylefeng.guns.modular.business.frequency.transportsign.controller;

import cn.stylefeng.guns.core.enums.ReturnCode;
import cn.stylefeng.guns.core.pojo.page.AndaPageResult;
import cn.stylefeng.guns.core.pojo.response.ResponseData;
import cn.stylefeng.guns.modular.business.frequency.orderlogistics.model.param.ErrorMsg;
import cn.stylefeng.guns.modular.business.frequency.ordersub.model.param.OrderDTO;
import cn.stylefeng.guns.modular.business.frequency.ordersub.model.result.PkgOrderSign;
import cn.stylefeng.guns.modular.business.frequency.statistics.OrderSignVO;
import cn.stylefeng.guns.modular.business.frequency.transportsign.entity.TransportSign;
import cn.stylefeng.guns.modular.business.frequency.transportsign.model.param.DeliverySignVO;
import cn.stylefeng.guns.modular.business.frequency.transportsign.model.param.SignRecordDTO;
import cn.stylefeng.guns.modular.business.frequency.transportsign.service.TransportSignService;
import cn.stylefeng.guns.modular.business.util.BusinessUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 运单签收记录表 控制器
 *
 * @author: yujie
 * @date: 2023/05/09 17:15
 */
@RestController
@RequestMapping("/api/sign")
public class TransportSignController {

    @Resource
    private TransportSignService signService;

    @Resource
    private BusinessUtil businessUtil;


    @ApiOperation(value = "待签收订单列表")
    @GetMapping("/wait")
    public ResponseData getWaitSignOrderList(OrderDTO orderDTO) {
        // 排序
        if (StringUtils.isBlank(orderDTO.getColumn())) {
            orderDTO.setColumn("orderId");
        }
        List<Long> empByOrgList = businessUtil.getEmpByOrgList();
        orderDTO.setDataList(empByOrgList);

        Page<OrderSignVO> list = signService.getWaitSignOrderList(orderDTO);
        return ResponseData.andaSuccess(new AndaPageResult<>().AndaPageResult(list, orderDTO));
    }

    @ApiOperation(value = "签收记录")
    @GetMapping("/record")
    public ResponseData getSignRecord(SignRecordDTO orderDTO) {
        // 排序
        if (StringUtils.isBlank(orderDTO.getColumn())) {
            orderDTO.setColumn("ts.create_time");
            orderDTO.setAsc(false);
        }
        List<Long> empByOrgList = businessUtil.getEmpByOrgList();
        orderDTO.setDataList(empByOrgList);

        Page<PkgOrderSign> list = signService.getSignRecord(orderDTO);
        return ResponseData.andaSuccess(new AndaPageResult<>().AndaPageResult(list, orderDTO));
    }

    @ApiOperation(value = "发运记录导出")
    @GetMapping("/export")
    public void export(HttpServletResponse response, SignRecordDTO orderDTO) throws Exception {
        // 排序
        if (StringUtils.isBlank(orderDTO.getColumn())) {
            orderDTO.setColumn("ts.create_time");
            orderDTO.setAsc(false);
        }
        List<Long> empByOrgList = businessUtil.getEmpByOrgList();
        orderDTO.setDataList(empByOrgList);

        signService.getExportSignRecord(orderDTO);
    }

    //@Log("运单到货批量签收")
    //@NoRepeatSubmit
    @ApiOperation(value = "批量签收")
    @PostMapping
    public ResponseData multiDeliverySign(@RequestBody DeliverySignVO deliverySignVO) {
        ErrorMsg errorMsg = new ErrorMsg();
        if (!signService.checkMultiSign(deliverySignVO, errorMsg)) {
            return ResponseData.andaError(ReturnCode.RETURN_PARAM_ERROR.code, errorMsg.getErrorMsg());
        }
        Integer integer = signService.multiSign(deliverySignVO);
        return ResponseData.andaSuccess(ReturnCode.RETURN_SUCCESS, integer);
    }

    @ApiOperation(value = "签收回退")
    @GetMapping("/reMultiDeliverySign/{orderId}")
    public ResponseData reMultiDeliverySign(@PathVariable Long id) {
        signService.reMultiSign(id);
        return ResponseData.andaSuccess();
    }

    //@Log("签收修改")
    //@NoRepeatSubmit
    @ApiOperation(value = "签收修改")
    @PutMapping
    public ResponseData orderSignUpdate(@RequestBody TransportSign transportSign) {
        ErrorMsg errorMsg = new ErrorMsg();
        if (!signService.checkSignUpdate(transportSign, errorMsg)) {
            return ResponseData.andaSuccess(ReturnCode.RETURN_PARAM_ERROR, null, errorMsg.getErrorMsg());
        }
        signService.updateSign(transportSign);
        return ResponseData.andaSuccess();
    }

    @ApiOperation(value = "签收实体详情")
    @GetMapping("/{id}")
    public ResponseData getSignPojoDetail(@PathVariable String id) {
        TransportSign orderSign = signService.getById(id);
        return ResponseData.andaSuccess(orderSign);
    }


}
